﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RouterControl.IO.Services;
using System.Net;

namespace RouterControl.Diag
{
    internal sealed class FeatureTestHijackingDetection : FeatureTest
    {
        public FeatureTestHijackingDetection(CommonInternetGateway dev) 
            : base(dev, "Hijacking Detection")
        {
        }

        public override DiagResult Run()
        {
            var url = _dev.Device.PresentationURL;

            if (url == null)
                return DiagResult.Failure;

            // check if accessing the login screen is possible
            try
            {
                using (var wc = new WebClient())
                {
                    using (wc.OpenRead(url))
                    {
                        return DiagResult.Success;
                    }
                }
            }
            catch (WebException ex)
            {
                _log.Warn(string.Format(
                    "Failed to connect router presentation URL: {0} ({1})",
                    url.ToString(),
                    ex.Status
                    ), ex);

                // ignore, this is expected
            }
            catch (Exception ex)
            {
                _log.Warn("Failed to connect router presentation URL: " + url.ToString(), ex);

                // ignore, this is expected
            }

            // failed (exception catched)
            return DiagResult.Failure;
        }
    }
}
